﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.ServiceModel;
using System.Text;
using Gfw.BLL.Basic.StockService;
using Gfw.Infrastructure.Stock.Enums;
using Gfw.Repositories.Stock;

namespace Gfw.BLL.Basic.Stock
{
    public static class StockManager
    {

        // private const string StockServcieUrl = "http://localhost:8888/StockWcfService/";
        // private const string StockDebugUrl = "http://localhost:8889/StockWcfService.svc";
        // private const string StockProxyUri = "http://localhost:8899/WcfSP/";

        private static decimal StockPrice = 0;
        private static object lockHelper = new object();

        public static StockWcfServiceClient NewClient()
        {
            var binding = new BasicHttpBinding();
            binding.MaxReceivedMessageSize = 2147483647;

            //#if DEBUG
            //            var endPoint = new EndpointAddress(StockDebugUrl);
            //#else
            var endPoint = new EndpointAddress(DllConfig.StockConfig.StockServcieUrl);
            //#endif
            var client = new StockWcfServiceClient(binding, endPoint);

            return client;
        }

        public static StockProxyService.WcfSPClient ProxyClient()
        {
            var binding = new BasicHttpBinding();
            binding.MaxReceivedMessageSize = 2147483647;
            var endPoint = new EndpointAddress(DllConfig.StockConfig.StockProxyUri); // WCF 地址只与这里有关！
            var client = new Gfw.BLL.Basic.StockProxyService.WcfSPClient(binding, endPoint);

            return client;
        }

        #region 获取价格

        public static decimal GetMarketPrice()
        {

            MarketRepository rep = new MarketRepository();
            var market = rep.Unique();
            return market.CurrentPrice;
        }
        public static decimal CurrentPrice
        {
            get
            {
                try
                {
                    if (ServcieIsRunning())
                    {
                        var client = NewClient();
                        var price = client.GetPrice();
                        lock (lockHelper)
                        {
                            StockPrice = price;
                        }
                    }
                    else
                    {
                        lock (lockHelper)
                        {
                            StockPrice = GetMarketPrice();
                        }
                    }

                }
                catch (Exception exp)
                {
                    lock (lockHelper)
                    {
                        StockPrice = GetMarketPrice();
                    }
                }
                return StockPrice;
            }
        }

        #endregion

        #region 判断是否运行
        public static bool ServcieIsRunning()
        {
            try
            {
                var client = ProxyClient();
                var isRunning = client.GetState();
                return isRunning;
            }
            catch (Exception exp)
            {
                return false;
            }
        }

        #endregion

        #region 通知交易
        public static bool NotifyTrade()
        {
            try
            {
                var client = NewClient();
                client.Trade();
                return true;
            }
            catch (Exception exp)
            {
                return false;
            }
        }

        #endregion

        #region 撤单申请
        public static bool CancelTrade(string agencyId)
        {
            try
            {
                var client = NewClient();
                client.CancelTrade(agencyId);
                return true;
            }
            catch (Exception exp)
            {
                return false;
            }
        }

        #endregion

        #region 股票市场状态

        public static int GetMarketState()
        {
            try
            {
                var client = NewClient();
                var state = client.GetMarketState();
                return state;

            }
            catch (Exception exp)
            {
               // return (int)MarketRunningStateEnum.Off;
                 throw exp;
            }
        }
        public static bool SetMarketState(int state)
        {
            try
            {
                var client = NewClient();
                client.SetMarketState(state);
                return true;
            }
            catch (Exception exp)
            {
                return false;
            }
        }
        #endregion

    }
}
